package action.submitwork;

import java.io.File;
import java.io.PrintWriter;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import model.StudentWork;
import model.Work;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;

import service.queryWorkService;
import service.submitWorkService;
import util.Upload;

import com.opensymphony.xwork2.ActionSupport;

@Namespace("")
@Action(
	value="submitWork",
	results={
			@Result(name="success",type="redirect",location="StuqueryAllWork"),
			@Result(name="fail",location="/WEB-INF/checkDetail.jsp" )
	}		
)
public class submitWork extends ActionSupport{
    
   private File file ; //具体上传文件的 引用 , 指向临时目录中的临时文件  
   private String fileFileName ;  // 上传文件的名字 ,FileName 固定的写法  
   private String fileContentType ; //上传文件的类型， ContentType 固定的写法  
   private int work_id; 
   
   public File getFile() {  
       return file;  
   }  
   public void setFile(File file) {  
       this.file = file;  
   }  
   public String getFileFileName() {  
       return fileFileName;  
   }  
   public void setFileFileName(String fileFileName) {  
       this.fileFileName = fileFileName;  
   }  
   public String getFileContentType() {  
       return fileContentType;  
   }  
   public void setFileContentType(String fileContentType) {  
       this.fileContentType = fileContentType;  
   }  
   
   public String execute() throws Exception {  
	   String FileType=Upload.getFileType(fileFileName);
	   if(FileType.equals("zip")||FileType.equals("rar")){
            String path = ServletActionContext.getRequest().getRealPath("/work"); //文件存储路径  
            StudentWork stuwork=new StudentWork();
            stuwork.setWork_id(work_id);
            HttpServletRequest request=ServletActionContext.getRequest();
            HttpSession session=request.getSession();
            String user_id=(String) session.getAttribute("user_id");
            stuwork.setStu_id(user_id);
            stuwork.setSubmittime(new Date());
            stuwork.setFilePath(path+"/"+fileFileName);
            submitWorkService submit=new  submitWorkService();
            submit.submitWork(stuwork, file, path, fileFileName);
            return SUCCESS; 
        }
	   else{
		   
		   Work work=new Work();
		   queryWorkService qws=new queryWorkService();
		   work=qws.querySingleWork(work_id);
		   HttpServletRequest request=ServletActionContext.getRequest();
		   request.setAttribute("submitmessage", "<script>alert('上传文件格式错误')</script>");
		   request.setAttribute("WorkDetail", work);
		   return "fail";  
	   }
   }
public int getWork_id() {
	return work_id;
}
public void setWork_id(int work_id) {
	this.work_id = work_id;
}  
    
	
}
